package acs.bridge;

import acs.bridge.dao.BridgeDAOImpl;
import acs.bridge.model.RawTableMail;
import org.apache.log4j.Logger;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.math.BigDecimal;

/**
 * home
 * ACS
 * User: kuznesovstv
 * Date: 24.05.13
 * Time: 17:54
 */
public class bm_mail implements BridgeModuleInterface, BridgeModuleExtInterface {
    Logger logger = Logger.getLogger(this.getClass().getName());
    public static BridgeDAOImpl bridgeDAOImpl;
    private acs.bridge.dao.bm_mailDAOImpl bm_mailDAOImpl;
    private JavaMailSenderImpl mailSender;

    public static void main(String[] args) {
    }

    public void setBridgeDAOImpl(BridgeDAOImpl bridgeDAOImpl) {
        this.bridgeDAOImpl = bridgeDAOImpl;
    }

    @Override
    public void execute(String a_group_tag, String a_meta_tag, BigDecimal a_raw_id, String a_module_tag) throws BridgeModuleException {
        logger.info("BridgeModule raw_id=" + a_raw_id);
        bm_mailDAOImpl.setTableName(bridgeDAOImpl.fnc_schema_by_group(a_group_tag, a_meta_tag) + "." + "FBI_RAW_" + a_meta_tag);

        RawTableMail rawTableMail = bm_mailDAOImpl.Select(a_raw_id);

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(rawTableMail.getSend_adr());

            helper.setTo(InternetAddress.parse(rawTableMail.getRecip_adr(), true));


            helper.setSubject(rawTableMail.getMail_subject());
            helper.setText(rawTableMail.getMail_body());


            helper.addAttachment(rawTableMail.getAttach_name(),
                    new ByteArrayResource(rawTableMail.getAttach_data()));
            logger.info("Send mail to " + rawTableMail.getRecip_adr());
            mailSender.send(message);
            logger.info("Mail has been sent");
        } catch (MessagingException e) {
            throw new BridgeModuleException(e.getMessage());
        }


    }

    @Override
    public void initial_processing() {
    }

    @Override
    public void execute_meta_start(String a_group_tag, String a_meta_tag) {
        logger.info("execute_meta_start");
    }

    public void execute_meta_finish(String a_group_tag, String a_meta_tag) {
        logger.info("execute_meta_finish");
    }

    @Override
    public void final_processing(BigDecimal bigDecimal) {
    }

    public void setBm_mailDAOImpl(acs.bridge.dao.bm_mailDAOImpl bm_mailDAOImpl) {
        this.bm_mailDAOImpl = bm_mailDAOImpl;
    }

    public acs.bridge.dao.bm_mailDAOImpl getBm_mailDAOImpl() {
        return bm_mailDAOImpl;
    }

    public void setMailSender(JavaMailSenderImpl mailSender) {
        this.mailSender = mailSender;
    }

    public JavaMailSenderImpl getMailSender() {
        return mailSender;
    }
}